<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class CommentsController extends AppController {

    var $name = 'Comments';
    var $components = array(
        'Session', 'Alias', 'Upload'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses   =   array(
        'Comment'
    );
    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - Hệ thống Comments');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'subject LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Comment.id' => 'DESC'
            ),
        );
        
        $data = $this->paginate('Comment');
        $this->set('total', $this->params['paging']['Comment']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }
    function approve_comment(){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://thegioigamevui.com/games/service_builtopcomment");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch); 
        curl_close($ch);
    }    
    function approved() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Hệ thống Comments');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'Comment.content LIKE' => "%$search%",
                'Comment.published' => "1",
            );
        }
        $conditions['AND'] = array(
            'Comment.published' => "1"
        );
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Comment.id' => 'DESC'
            ),
        );

        $data = $this->paginate('Comment');
        $this->set('total', $this->params['paging']['Comment']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }
    function unapproved(){
        $this->set('title_for_layout', Configure::read('site_name') . ' - Hệ thống Comments');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'Comment.content LIKE' => "%$search%",
            );
        }
        $conditions['AND'] = array(
            'Comment.published' => "0"
        );
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Comment.id' => 'DESC'
            ),
        );

        $data = $this->paginate('Comment');
        $this->set('total', $this->params['paging']['Comment']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }
    function remove() {
        $conditions = array(
            "{$this->Comment->alias}.{$this->Comment->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );

        if ($this->Comment->deleteAll($conditions)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d Comments", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Comment->id = $params['cid'][0];
        } else {
            $this->Comment->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }

        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');

        if (empty($this->data)) {
            $fields = array(
                '*'
            );

            $this->data = $this->Comment->read($fields);
        }
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm Comment');
    }

    function save() {
        $data = $this->data['Comment'];
       
        if(isset($data['id'])){
            $data['user_modified']   =   $this->Auth->user('id');
            $data['user_modified_ip']   =   $_SERVER['REMOTE_ADDR'];
        }
        else{
            $data['user_created']   =    $this->Auth->user('id');
            $data['user_created_ip']   =    $_SERVER['REMOTE_ADDR'];
        }

        if ($this->Comment->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Comment->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'comments', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'comments', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $this->Comment->id = $id;
            $data = $this->Comment->read($fields);
            $str_search =   array(
                ':))',
                ':)',
                '=))',
                ':((',
                ':d',
                ':D',
                ':(',
                ':-?',
                ':|',
                ';))',
                ';)',
                ':x',
                
            );
            $str_replace =   array(
                '<img class="icon" src="/img/smiles/laughing_icon.gif" alt="laughing icon" />',
                '<img class="icon" src="/img/smiles/happy_icon.gif" alt="happy icon" />',
                '<img class="icon" src="/img/smiles/rollingonthefloor.gif" alt="rolling on the floor" />',
                '<img class="icon" src="/img/smiles/crying.gif" alt="crying" />',
                '<img class="icon" src="/img/smiles/biggrin_icon.gif" alt="big grin" />',
                '<img class="icon" src="/img/smiles/biggrin_icon.gif" alt="big grin" />',
                '<img class="icon" src="/img/smiles/sad_icon.gif" alt="Sad" />',
                '<img class="icon" src="/img/smiles/thinking.gif" alt="thinking" />',
                '<img class="icon" src="/img/smiles/straightface_icon.gif" alt="straight face" />',
                '<img class="icon" src="/img/smiles/heehee.gif" alt="hee hee" />',
                '<img class="icon" src="/img/smiles/winking_icon.gif" alt="winking" />',
                '<img class="icon" src="/img/smiles/lovestruck_icon.gif" alt="love struck" />',
                
            );
            
            $data['Comment']['content'] =   str_replace($str_search, $str_replace, $data['Comment']['content']);
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
                'content'   =>  $data['Comment']['content'],
                'user_approved' =>  $this->Auth->user('id'),
                'user_modified' =>  $this->Auth->user('id'),
                'user_modified_ip'=>    $_SERVER['REMOTE_ADDR']
            );
        }
        
        if ($this->Comment->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Comment->invalidFields(), 'error', array(), 'msg');
        }
        $this->approve_comment();
        $this->redirect(array('controller' => 'comments', 'action'=>'approved'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
                'user_modified' =>  $this->Auth->user('id'),
                'user_modified_ip'=>    $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Comment->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Comment->invalidFields(), 'error', array(), 'msg');
        }
        $this->approve_comment();
        $this->redirect(array('controller' => 'comments','action'=>'unapproved'));
    }

}

?>
